package com.modules.system.service.impl;


import com.common.base.BaseServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.constant.Constants;
import com.common.util.StringUtil;
import com.shiro.ShiroUtils;
import com.modules.system.dao.SysMenuDao;
import com.modules.system.dao.SysUserDao;
import com.modules.system.entity.SysMenuEntity;
import com.modules.system.entity.SysUserEntity;
import com.modules.system.service.SysUserRoleService;
import com.modules.system.service.SysUserService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 系统用户
 * 
 */
@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUserEntity> implements SysUserService  {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysMenuDao sysMenuDao;

	@Autowired
	private void setBaseMapper(){
		super.setBaseMapper(sysUserDao);
	}

	@Override
	public List<String> getAllPerms(Long userId) {
		return sysUserDao.getAllPerms(userId);
	}

	@Override
	public List<Long> getAllMenuId(Long userId) {
		return sysUserDao.getAllMenuId(userId);
	}

//	@Override
//	public SysUserEntity getByUserName(String username) {
//		return sysUserDao.getByUserName(username);
//	}
	
	@Override
	public SysUserEntity getObject(Long userId) {
		return sysUserDao.selectByPrimaryKey(userId);
	}

	@Override
	public List<SysUserEntity> getList(String username){
		return sysUserDao.getList(username);
	}
	

	@Override
	@Transactional
	public void save(SysUserEntity user) {
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		user.setSalt(salt);
		user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
		sysUserDao.insert(user);
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	@Transactional
	public void update(SysUserEntity user) {
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
		}
		sysUserDao.updateByPrimaryKey(user);
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	@Transactional
	public void deleteBatch(Long[] userId) {
		sysUserDao.deleteBatch(userId);
	}

//	@Override
//	public int updatePassword(Long userId, String password, String newPassword) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("userId", userId);
//		map.put("password", password);
//		map.put("newPassword", newPassword);
//		return sysUserDao.updatePassword(map);
//	}

	@Override
	public Set<String> getUserPermissions(Long userId) {
		List<String> permsList = null;
		
		//系统管理员，拥有最高权限
		if(userId == Constants.SUPER_ADMIN){
			List<SysMenuEntity> menuList = sysMenuDao.getListWithParent();
			permsList = new ArrayList<String>(menuList.size());
			for(SysMenuEntity menu : menuList){
				permsList.add(menu.getPerms());
			}
		}else{
			permsList = sysUserDao.getAllPerms(userId);
		}

		//用户权限列表
		Set<String> permsSet = new HashSet<String>();
		for(String perms : permsList){
			if(StringUtil.isNullString(perms)){
				continue;
			}
			permsSet.addAll(Arrays.asList(perms.trim().split(",")));
		}
		return permsSet;
	}

}
